package com.hifar.test.unitstandardrelation.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.hifar.test.standardbase.pojo.HfResStandard;
import com.hifar.test.unitstandardrelation.mapper.HfPrjUnitStandardMapper;
import com.hifar.test.unitstandardrelation.pojo.HfPrjUnitStandard;
import com.hifar.test.unitstandardrelation.pojo.HfPrjUnitStandardVo;
import com.hifar.test.unitstandardrelation.service.IHfPrjUnitStandardService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.fw.utils.id.IdUtils;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;

/**
 * <p>
 * 项目-检测项目-标准 服务实现类
 * </p>
 *
 * @author wangkepeng
 * @since 2024-12-05 09:22:13
 */
@Service
public class HfPrjUnitStandardServiceImpl extends ServiceImpl<HfPrjUnitStandardMapper, HfPrjUnitStandard> implements IHfPrjUnitStandardService {


	@Override
	public BusiResponse exportExcel(Map<String, Object> pageInfo) {
		BusiResponse busiResponse = new BusiResponse();
		String ids = (String) pageInfo.get("ids");

		String operationType = (String) pageInfo.get("operationType");

		if (StringUtils.isNotBlank(operationType) && (operationType.equals("template"))) {
			ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfPrjUnitStandard.class, "导入模板", "项目-检测项目-标准导入数据");
			return busiResponse;
		} else {
			QueryWrapper<HfPrjUnitStandard> postParam = RequestConver.getPostParam(pageInfo);
			postParam.ne("del_flag", 1);
			postParam.orderByDesc("create_time");
			if (StringUtils.isNotBlank(ids)) {
				postParam.in("id", ids);
			}
			List<HfPrjUnitStandard> dataList = this.list(postParam);
			if (dataList != null && dataList.size() > 0) {
				ExcelTemplateUtil.exportExcel(busiResponse, dataList, HfPrjUnitStandard.class, "项目-检测项目-标准数据", "项目-检测项目-标准导出");
			} else {
				ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfPrjUnitStandard.class, "项目-检测项目-标准数据", "项目-检测项目-标准导出");
			}
			return busiResponse;
		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public BusiResponse importExcel(MultipartFile file) throws Exception {
		if (file != null && !file.isEmpty()) {
			List<HfPrjUnitStandard> importExcel = ExcelTemplateUtil.importExcel(file.getInputStream(), HfPrjUnitStandard.class);
			List<HfPrjUnitStandard> importExcelList = new ArrayList<>();
			if (importExcel != null && importExcel.size() > 0) {
				for (HfPrjUnitStandard bean : importExcel) {
					bean.setId(IdUtils.uuid32());
//					bean.setDelFlag(0);
					//if(!bean.getName().equals("")) {
					importExcelList.add(bean);
					//}
				}
				this.saveBatch(importExcelList);
				return BusiResponse.ok("导入成功！");
			}
		}
		return BusiResponse.error("导入失败，原因未知！");
	}

	@Override
	public List<HfResStandard> listUnitStandard(String id) {
		return baseMapper.selectUnitStandardList(id);
	}
	
	@Override
	public Page<HfResStandard> pageHfResStandardVo(Page<HfResStandard> rowPage,QueryWrapper<HfResStandard> wrapper){
		 return this.getBaseMapper().pageHfResStandardVo(rowPage,wrapper);
	 }
	
	@Override
	public Page<HfPrjUnitStandardVo> pageHfPrjUnitStandardVo(Page<HfPrjUnitStandardVo> rowPage, QueryWrapper<HfPrjUnitStandardVo> wrapper){
		 return this.getBaseMapper().pageHfPrjUnitStandardVo(rowPage,wrapper);
	 }
	@Override
	public List<HfPrjUnitStandardVo> listHfPrjUnitStandardVo(QueryWrapper<HfPrjUnitStandardVo> wrapper){
		 return this.getBaseMapper().listHfPrjUnitStandardVo(wrapper);
	 }
	 
}
